package com.example.a16021.myapplication;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class StorageActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_storage);
        System.out.println(isExistSdCard());
        System.out.println(getSdPath());
        System.out.println(isExistSdCard());
        System.out.println(getSdPath());
        readAssets();


    }
    public  boolean isExistSdCard(){
        String stae=Environment.getExternalStorageState();
        System.out.println(stae);
        if (stae.equals(Environment.MEDIA_MOUNTED)){
            return true;
        }else {
            return false;
        }
    }
    public  String getSdPath(){
        String path=Environment.getExternalStorageDirectory().getAbsolutePath();
        return path;
    }
    public void readAssets(){
        try {
           InputStream inputStream= getAssets().open("city.txt");
            InputStreamReader inputStreamReader=new InputStreamReader(inputStream,"utf-8");
            BufferedReader reader=new BufferedReader(inputStreamReader);
            String s=reader.readLine();
            System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public  void readSdFile(){
        String filePath=Environment.getExternalStorageDirectory().getAbsolutePath()+"text.txt";
        File file= new File(filePath);
        try {
            FileInputStream fileInputStream=new FileInputStream(file);
            int len=fileInputStream.available();
            byte[] bytes=new  byte[len];
            fileInputStream.read(bytes);
            String s=new String(bytes);
            System.out.println("-----"+s);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
